package com.zr.task.activity;

import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.View;
import android.widget.LinearLayout;

import com.jietong.rfid.util.EPC;
import com.zh.baselibrary.base.BaseNoIMActivity;
import com.zh.baselibrary.constant.BaseConstant;
import com.zh.baselibrary.netutils.NetUtil;
import com.zh.baselibrary.utils.BarUtil;
import com.zh.baselibrary.utils.ClickUtils;
import com.zh.baselibrary.utils.JsonUtils;
import com.zh.baselibrary.utils.LogUtils;
import com.zh.baselibrary.utils.PhoneUtil;
import com.zh.baselibrary.utils.ShareManager;
import com.zh.baselibrary.views.ToastView;
import com.zh.model.task.home.ClassCabinetBean;
import com.zh.model.task.home.RemoteOpenResVO;
import com.zh.model.task.subject.LoginResBean;
import com.zr.andealib.util.HardEvent;
import com.zr.andealib.util.constant.AndeaConstant;
import com.zr.task.R;
import com.zr.task.adapter.HomeCabinetAdapter;
import com.zr.task.constant.Constant;
import com.zr.task.dialog.ManagerVerificationDialog;
import com.zr.task.presenter.TaskMainPresenter;
import com.zr.task.presenter.view.TaskMainView;
import com.zr.task.utils.EquipmentTypeUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.OnLongClick;

public class BidActivity extends BaseNoIMActivity<TaskMainPresenter> implements  TaskMainView {
    @BindView(R.id.hint_layout)
    LinearLayout hintLayout;

    private int color;
    private ManagerVerificationDialog mVerificationDialog;
    private HomeCabinetAdapter mAdapter;
    private Integer clickType = 0;
    private boolean isStop = false;

    @Override
    protected int getLayoutId() {
        int type = ShareManager.getInt(this, Constant.BID_TYPE);
        switch (type) {
            case 1:
                color = R.color.company1;
                return R.layout.activity_bid1;
            case 2:
                color = R.color.company2;
                return R.layout.activity_bid2;
            case 3:
                color = R.color.company3;
                return R.layout.activity_bid3;
            default:
                color = R.color.company1;
                return R.layout.activity_bid1;

        }
    }
    BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {

            if (intent.getAction().equals(Constant.BROAT_CAST_ACTION_HOME_FRESH)) {
                mPresenter.getCabinetDetail();
//                ClassCabinetBean mBean = JsonUtils.stringToObject(ShareManager.getValue(TaskMainActivity.this, BaseConstant.LOCAL_CAB_LIST), ClassCabinetBean.class);
//                upViewHome(mBean);

            } else if (intent.getAction().equals(Constant.BROAT_CAST_ACTION_ERROR_COMPLTE)) {
                mPresenter.errorComplete(intent);

            } else if (intent.getAction().equals(BaseConstant.BROAT_CAST_ACTION_EQUIPMENT_UPDATE)) {
                ClassCabinetBean mBean = JsonUtils.stringToObject(ShareManager.getValue(BidActivity.this, BaseConstant.LOCAL_CAB_LIST), ClassCabinetBean.class);
                upViewHome(mBean);
            }
        }
    };
    @Override
    protected void init() {
        EventBus.getDefault().register(this);
        if(!EquipmentTypeUtils.hasRf(this))
            hintLayout.setVisibility(View.GONE);
        IntentFilter mIntentFilter = new IntentFilter();
        mIntentFilter.addAction(Constant.BROAT_CAST_ACTION_HOME_FRESH);
        mIntentFilter.addAction(Constant.BROAT_CAST_ACTION_ERROR_COMPLTE);
        mIntentFilter.addAction(BaseConstant.BROAT_CAST_ACTION_EQUIPMENT_UPDATE);
        registerReceiver(broadcastReceiver, mIntentFilter);
        mPresenter.setColor(color);
        mPresenter.initDevice();
        mPresenter.getCabinetDetail();
        mAdapter = new HomeCabinetAdapter(new ArrayList<>());
    }
    public void onViewClicked(View view) {
        if (ClickUtils.isFastClick()) {
            switch (view.getId()){
                case R.id.take:
                    clickType = 1;
                    break;
                case R.id.mistake:
                    clickType = 2;
                    break;
                case R.id.relation:
                    clickType = 3;
                    break;
            }
            showDialog();
        }
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void Event(HardEvent hardEvent) {
        int flag = hardEvent.getFlag();
        if (flag == AndeaConstant.CALL_VERIFICATION_SUCCSES) {  //磁卡登录
            String code = (String) hardEvent.getData();
            if (clickType == 1) {
                mPresenter.checkCard(code);
            } else if (clickType == 2) {
                mPresenter.errataSwipe(code);
            } else if (clickType == 3) {
                mPresenter.swipingCard(code);
            }
        } else if (flag == AndeaConstant.CALL_LOCK_STATUS_SUCCSES) { //门开启
            mPresenter.checkDoorClose((Integer) hardEvent.getData(),true);
        } else if (flag == AndeaConstant.CALL_LOCK_STATUS_ERROR || flag == AndeaConstant.CALL_READ_CHECK_CLOSE_ERROR || flag == AndeaConstant.CALL_INIT_CHECK_ERROR) {
            String tip = (String) hardEvent.getData();
            ToastView.showLong((String) hardEvent.getData());
            if (flag == AndeaConstant.CALL_LOCK_STATUS_ERROR) {
                mPresenter.openResult(0,1);
                if(tip.equals("柜门处于开启状态")) {
                    mPresenter.checkDoorClose(mPresenter.getOpenNum(), false);
                }
            }
        } else if (flag == AndeaConstant.CALL_READ_CHECK_SUCCSES) { // 盘点数据
            EPC epcTag = (EPC) hardEvent.getData();
            mPresenter.commit(epcTag);
        }else if (flag == AndeaConstant.CALL_REMOTE_OPEN) { //远程开启
            mPresenter.remoteOpen((RemoteOpenResVO) hardEvent.getData());
        } else if (flag == AndeaConstant.CALL_EMERGENCY) { //应急
            mPresenter.complete(true,(Integer) hardEvent.getData());
            if(!isStop)
                mPresenter.beginInvV2();
        }else if (flag == AndeaConstant.CALL_CORRIGEDUM) { //勘误
            if(!isStop)
                mPresenter.beginInvV2();
        }
    }
    @OnLongClick(R.id.title_bid)
    public boolean onLongClick(View view) {
        AlertDialog alertDialog = new AlertDialog
                .Builder(this)
                .setItems(new String[]{"标一", "标二", "标三","退出"}, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if(which==3){
                            BarUtil.showBar();
                            finish();
                            return;
                        }
                        ShareManager.setInt(BidActivity.this, Constant.BID_TYPE,++which);
                        System.exit(0);
                    }
                }).create();
        alertDialog.show();
        return  true;
    }
    private void showDialog() {
        mPresenter.stopInvV2();
        mVerificationDialog = new ManagerVerificationDialog(this, R.style.DialogTheme, new ManagerVerificationDialog.OnCloseListener() {
            @Override
            public void OnClose() {
                mPresenter.beginInvV2();
                dismissDialog();
            }
        });
        mVerificationDialog.setShowQRCode(false);
        mVerificationDialog.show();
    }
    @Override
    protected TaskMainPresenter createPresenter() {
        return new TaskMainPresenter(ShareManager.getValue(this, BaseConstant.DEVICE_SCHOOL_ID), PhoneUtil.getUUID(this));
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        if (broadcastReceiver != null) {
            unregisterReceiver(broadcastReceiver);
        }
    }

    @Override
    public int getClickType() {
        return clickType;
    }

    @Override
    public void setClickType(int type) {

    }

    @Override
    public void doorClose(String tip) {
        mPresenter.beginInvV2();
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                ToastView.showLong(tip);
            }
        });

    }

    @Override
    public void dismissDialog() {
        if (mVerificationDialog != null) {
            mVerificationDialog.cancel();
        }
    }
    @Override
    public void upViewHome(ClassCabinetBean cabinetBean) {   mAdapter.clearAdd(cabinetBean.getCabinetDetailList()); }
    @Override
    public HomeCabinetAdapter getAdapter() { return mAdapter; }
    @Override
    public void initSubject(LoginResBean loginResBean) {
        ToastView.showShort("标柜不唯一");
    }
    @Override
    public void hideSubject() { }

    @Override
    public void initMore() {
        if(EquipmentTypeUtils.hasRf(this))
            hintLayout.setVisibility(View.VISIBLE);
        else
            hintLayout.setVisibility(View.GONE);
    }
    @Override
    protected void onResume() {
        super.onResume();
        isStop = false;
        mPresenter.beginInvV2();
    }

    @Override
    protected void onStop() {
        super.onStop();
        isStop = true;
        if(mPresenter!=null)
            mPresenter.stopInvV2();
    }

    @Override
    public void onHint(String hint) {
        super.onHint(hint);
        ToastView.showLong(hint);
        dismissDialog();
        mPresenter.beginInvV2();
    }
}
